package com.corpgovernment.common.dataloader;

import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.common.dto.GetOrderInfoDtoReq;
import com.corpgovernment.common.dto.GetOrderInfoDtoRsp;
import com.corpgovernment.common.dto.OrderUserInfoRsp;
import com.corpgovernment.common.feignRpc.CommonOrderCenterClient;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("commonOrderCenterDataloader")
public class CommonOrderCenterDataloader {

    private final static String EMPTY = "null";

    @Autowired
    private CommonOrderCenterClient commonOrderCenterClient;

    public GetOrderInfoDtoRsp getOrderInfoBySupplierOrderId(String supplierOrderId) {
        if (StringUtils.isBlank(supplierOrderId)) {
            return null;
        }
        if (supplierOrderId.length() >= 13 && supplierOrderId.contains("0000")) {
            return null;
        }
        GetOrderInfoDtoReq getOrderInfoDtoReq = new GetOrderInfoDtoReq();
        getOrderInfoDtoReq.setSupplierOrderId(supplierOrderId);
        JSONResult<GetOrderInfoDtoRsp> data = commonOrderCenterClient.getOrderInfoBySupplierOrderId(getOrderInfoDtoReq);
        if (Objects.isNull(data) || !data.isSUCCESS()) {
            log.info("getOrderInfoBySupplierOrderId error:{}", JsonUtils.toJsonString(data));
            return null;
        }
        return data.getData();
    }

    /**
     * 获取订单用户关联信息
     * @param orderId
     * @return
     */
    public OrderUserInfoRsp getOrderUser(String orderId) {
        if (StringUtils.isBlank(orderId) || EMPTY.equals(orderId)) {
            return null;
        }

        try {
            JSONResult<OrderUserInfoRsp> data = commonOrderCenterClient.getOrderUser(Long.parseLong(orderId));
            if (Objects.isNull(data) || !data.isSUCCESS()) {
                log.info("getOrderUser error:{}", JsonUtils.toJsonString(data));
                return null;
            }
            return data.getData();
        } catch (Exception e) {
            log.info("getOrderUser error", e);
        }
        return null;
    }
}
